package com.goldze.mvvmhabit.data;

import android.support.annotation.NonNull;
import android.support.annotation.VisibleForTesting;

import com.goldze.mvvmhabit.data.source.HttpDataSource;
import com.goldze.mvvmhabit.data.source.LocalDataSource;
import com.goldze.mvvmhabit.entity.DemoEntity;
import com.goldze.mvvmhabit.entity.response.CompanyResponseEntity;
import com.goldze.mvvmhabit.entity.response.GetBannerListResponseEntity;
import com.goldze.mvvmhabit.entity.response.GetChatUsersResponseEntity;
import com.goldze.mvvmhabit.entity.response.GetHistoryMsgResponseEntity;
import com.goldze.mvvmhabit.entity.response.GetLeaveListResponseEntity;
import com.goldze.mvvmhabit.entity.response.GetNewMsgResponseEntity;
import com.goldze.mvvmhabit.entity.response.GetTypeListResponseEntity;
import com.goldze.mvvmhabit.entity.response.LoginResponseEntity;
import com.goldze.mvvmhabit.entity.response.RegisterResponseEntity;
import com.goldze.mvvmhabit.entity.response.SelectDepartmentResponseEntity;
import com.goldze.mvvmhabit.entity.response.UserInfoResponseEntity;
import com.goldze.mvvmhabit.entity.response.VersionResponseEntity;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import me.goldze.mvvmhabit.base.BaseModel;
import me.goldze.mvvmhabit.http.BaseResponse;

/**
 * MVVM的Model层，统一模块的数据仓库，包含网络数据和本地数据（一个应用可以有多个Repositor）
 * Created by goldze on 2019/3/26.
 */
public class DemoRepository extends BaseModel implements HttpDataSource, LocalDataSource {
    private volatile static DemoRepository INSTANCE = null;
    private final HttpDataSource mHttpDataSource;

    private final LocalDataSource mLocalDataSource;

    private DemoRepository(@NonNull HttpDataSource httpDataSource,
                           @NonNull LocalDataSource localDataSource) {
        this.mHttpDataSource = httpDataSource;
        this.mLocalDataSource = localDataSource;
    }

    public static DemoRepository getInstance(HttpDataSource httpDataSource,
                                             LocalDataSource localDataSource) {
        if (INSTANCE == null) {
            synchronized (DemoRepository.class) {
                if (INSTANCE == null) {
                    INSTANCE = new DemoRepository(httpDataSource, localDataSource);
                }
            }
        }
        return INSTANCE;
    }

    @VisibleForTesting
    public static void destroyInstance() {
        INSTANCE = null;
    }


    @Override
    public Observable<Object> login() {
        return mHttpDataSource.login();
    }

    @Override
    public Observable<DemoEntity> loadMore() {
        return mHttpDataSource.loadMore();
    }

    @Override
    public Observable<BaseResponse<DemoEntity>> demoGet() {
        return mHttpDataSource.demoGet();
    }

    @Override
    public Observable<BaseResponse<DemoEntity>> demoPost(String catalog) {
        return mHttpDataSource.demoPost(catalog);
    }

    @Override
    public Observable<BaseResponse<LoginResponseEntity>> loginbypassword(String phone, String passWord) {
        return mHttpDataSource.loginbypassword(phone, passWord);
    }

    @Override
    public Observable<BaseResponse<UserInfoResponseEntity>> getUserInfo() {
        return mHttpDataSource.getUserInfo();
    }

    @Override
    public Observable<BaseResponse<RegisterResponseEntity>> register(String phone, String password, String name,
                                                                    String settingPassword) {
        return mHttpDataSource.register(phone, password, name, settingPassword);
    }

    @Override
    public Observable<BaseResponse<Boolean>> requestChangePassword(String oldPassWord, String newPassWord) {
        return mHttpDataSource.requestChangePassword(oldPassWord, newPassWord);
    }

    @Override
    public Observable<BaseResponse<String>> forgetPwd(String phone, String code, String passWord) {
        return mHttpDataSource.forgetPwd(phone, code, passWord);
    }

    @Override
    public Observable<BaseResponse<Boolean>> getModifyDepart(String id) {
        return mHttpDataSource.getModifyDepart(id);
    }

    @Override
    public Observable<BaseResponse<Boolean>> setUserSex(String sex) {
        return mHttpDataSource.setUserSex(sex);
    }

    @Override
    public Observable<BaseResponse<String>> sendSmsCode(String phone) {
        return mHttpDataSource.sendSmsCode(phone);
    }

    @Override
    public Observable<BaseResponse<GetLeaveListResponseEntity>> getLeaveList(int currentPage, int pageSize) {
        return mHttpDataSource.getLeaveList(currentPage, pageSize);
    }

    @Override
    public Observable<BaseResponse<List<SelectDepartmentResponseEntity>>> getDepartList() {
        return mHttpDataSource.getDepartList();
    }

    @Override
    public Observable<BaseResponse<ArrayList<GetBannerListResponseEntity>>> getGetBannerList() {
        return mHttpDataSource.getGetBannerList();
    }

    @Override
    public Observable<BaseResponse<String>> addLeaveForm(String starTime, String endTime, String reason,
                                                         String typeId, String image, String address) {
        return mHttpDataSource.addLeaveForm(starTime, endTime, reason, typeId, image, address);
    }

    @Override
    public Observable<BaseResponse<List<GetChatUsersResponseEntity>>> getChatUsers() {
        return mHttpDataSource.getChatUsers();
    }

    @Override
    public Observable<BaseResponse<String>> DelectUsers(String friendId) {
        return mHttpDataSource.DelectUsers(friendId);
    }


    @Override
    public Observable<BaseResponse<List<GetHistoryMsgResponseEntity>>> getHistoryMsg(String receiverId,
                                                                                     String endTime,
                                                                                     String startTime) {
        return mHttpDataSource.getHistoryMsg(receiverId, endTime, startTime);
    }

    @Override
    public Observable<BaseResponse<String>> sendMsg(String receiverId,
                                                    String content,
                                                    String messageType) {
        return mHttpDataSource.sendMsg(receiverId, content, messageType);
    }

    @Override
    public Observable<BaseResponse<String>> setHasRead(String senderId) {
        return mHttpDataSource.setHasRead(senderId);
    }

    @Override
    public Observable<BaseResponse<List<GetNewMsgResponseEntity>>> getNewMsg() {
        return mHttpDataSource.getNewMsg();
    }

    @Override
    public Observable<BaseResponse<ArrayList<GetTypeListResponseEntity>>> getTypeList() {
        return mHttpDataSource.getTypeList();
    }

    @Override
    public Observable<BaseResponse<String>> uploadImg(String urlPath) {
        return mHttpDataSource.uploadImg(urlPath);
    }

    @Override
    public Observable<BaseResponse<Boolean>> uploadHead(String urlPath) {
        return mHttpDataSource.uploadHead(urlPath);
    }

    @Override
    public Observable<BaseResponse<String>> getCompanyInfo() {
        return mHttpDataSource.getCompanyInfo();
    }

    @Override
    public Observable<BaseResponse<CompanyResponseEntity>> getCompany() {
        return mHttpDataSource.getCompany();
    }

    @Override
    public Observable<BaseResponse<VersionResponseEntity>> getCheckVersion() {
        return mHttpDataSource.getCheckVersion();
    }

    @Override
    public void saveUserName(String userName) {
        mLocalDataSource.saveUserName(userName);
    }

    @Override
    public void savePassword(String password) {
        mLocalDataSource.savePassword(password);
    }

    @Override
    public String getUserName() {
        return mLocalDataSource.getUserName();
    }

    @Override
    public String getPassword() {
        return mLocalDataSource.getPassword();
    }
}
